package edu.rice.myxo.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Vector;

import edu.rice.myxo.sim.MyxoAgent;

public class IOutils {

	public void writeAgents(ArrayList<MyxoAgent> l, String dirname) {
		// /////////////////////////
		String fn;
	}

	public void writeMatrix(double[][] m, String dirname, String matrixname) {
		String filename = dirname + "//" + matrixname + ".txt";
		try {
			FileWriter fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			for (int i = 0; i < m.length; i++) {
				for (int j = 0; j < m[0].length; j++) {
					pw.print(m[i][j] + ",");
				}
				pw.println();
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
		}
	}

	public void writeMatrix(int[][] m, String dirname, String matrixname) {
		String filename = dirname + "//" + matrixname + ".txt";
		try {
			FileWriter fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			for (int i = 0; i < m.length; i++) {
				for (int j = 0; j < m[0].length; j++) {
					pw.print(m[i][j] + ",");
				}
				pw.println();
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
		}
	}

	public void writeArray(double[] array, String dirname, String arrayname) {
		String filename = dirname + "//" + arrayname + ".txt";
		try {
			FileWriter fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			for (int i = 0; i < array.length; i++) {
				pw.println(array[i]);
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
		}
	}

	public void writePartArray(double[] array, String dirname,
			String arrayName, int start, int end) throws IOException {
		String filename = dirname + "//" + arrayName + ".txt";
		FileWriter fw = new FileWriter(filename);
		PrintWriter pw = new PrintWriter(fw);
		for (int i = start; i <= end; i++) {
			pw.println(array[i]);
		}
		pw.close();
		fw.close();
	}

	public void writeArray(int[] array, String dirname, String arrayname) {
		String filename = dirname + "//" + arrayname + ".txt";
		try {
			FileWriter fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			for (int i = 0; i < array.length; i++) {
				pw.println(array[i]);
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
		}
	}

	public void writeList(ArrayList<Double> list, String dirname,
			String listname) {
		String filename = dirname + "//" + listname + ".txt";
		FileWriter fw;
		try {
			fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			for (int i = 0; i < list.size(); i++) {
				pw.println(list.get(i));
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void writeList(Vector<Double> list, String dirname, String listname) {
		String filename = dirname + "//" + listname + ".txt";
		FileWriter fw;
		try {
			fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			for (int i = 0; i < list.size(); i++) {
				pw.println(list.get(i));
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Rule = 0 output all Rule = 1 output none 0 members Rule = 2 output
	 * greater than min members
	 * 
	 * @param array
	 * @param dirName
	 * @param arrayName
	 * @param rule
	 * @throws IOException
	 */
	public void writePartialArray(double[] array, String dirName,
			String arrayName, int rule, double min) {
		String filename = dirName + "//" + "stats" + "." + arrayName + ".txt";
		try {
			FileWriter fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			switch (rule) {
			case 0:
				writeArray(array, dirName, arrayName);
				break;
			case 1:
				for (int i = 0; i < array.length; i++) {
					if (array[i] != 0) {
						pw.println(array[i]);
					}
				}
				break;
			case 2:
				for (int i = 0; i < array.length; i++) {
					if (array[i] > min) {
						pw.println(array[i]);
					}
				}
			default:
				writeArray(array, dirName, arrayName);
				break;
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
		}
	}

	public void writePartialArray(int[] array, String dirName,
			String arrayName, int rule, int min) {
		String filename = dirName + "//" + "stats" + "." + arrayName + ".txt";
		try {
			FileWriter fw = new FileWriter(filename);
			PrintWriter pw = new PrintWriter(fw);
			switch (rule) {
			case 0:
				writeArray(array, dirName, arrayName);
				break;
			case 1:
				for (int i = 0; i < array.length; i++) {
					if (array[i] != 0) {
						pw.println(array[i]);
					}
				}
				break;
			case 2:
				for (int i = 0; i < array.length; i++) {
					if (array[i] > min) {
						pw.println(array[i]);
					}
				}
			default:
				writeArray(array, dirName, arrayName);
				break;
			}
			pw.close();
			fw.close();
		} catch (IOException e) {
		}
	}
    public void writeReversalPoints(ArrayList<double[]>[] l, String dirname){
	try{
	    for(int i = 0; i < l.length; i++){
		String fn = "cell"+i;
		PrintWriter pw = new PrintWriter(new FileWriter(fn));
		double[] tmp = l[i].get(i);
		for(int j = 0; j < tmp.length; j++){
		    pw.print(tmp[j] + " ");
		}
		pw.println();
		pw.close();
	    }
	}catch(IOException e){
	}
    }
}
